<?php
/**
 * @copyright: ©2024 永州 微聚 商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */
namespace app\api\controller\consume;
use app\admin\model\ActivityConsumer;
use app\admin\model\StoreJoinActivity;
use app\common\controller\AdminController;
use app\mobile\service\WechatpayService;
use think\App;

/**
 * @ControllerAnnotation(title="会员管理")
 */
class Template extends AdminController
{
    
    use \app\admin\traits\Curd;

    /**
     * 字段排序
     *
     * @var array
     */
    protected $sort = [
        'create_time' => 'desc'
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        
        $this->model = new \app\admin\model\Template();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $request = $this->request;
        if ($this->request->platform_id >0 ){
            $where[] = ['platform_id','=',$this->request->platform_id];
        }
        $data = $this->model->withJoin('platformInfo', 'LEFT')
            ->where($where)
            ->select()->toArray();
        if ($data){
            $data =  array_column($data,'template_id','name');
        }else{
            $data=[
                'withdraw_result_template' =>'',
                'ticket' =>'',
                'claim_consume' =>'',
                'consume_expire' =>'',
                'ticket_review' => '',
                'store' => '',
                'payment' => ''
                ];
        }
        $platform = new \app\admin\model\Platform();
        $platformInfo = $platform->where(['id' => $this->request->platform_id])->find();
        $list = $this->gettemplatelist($platformInfo['xcx_appid'],$platformInfo['xcx_appsecret']);
        $industry = $this->getindustry($platformInfo['xcx_appid'],$platformInfo['xcx_appsecret']);
        $industrytext = '';
        if ($industry && is_array($industry)) {
            foreach ($industry as $indu) {
                if ($indu && is_array($indu)) {
                    foreach ($indu as $i) {
                        $industrytext .= $i['name'] . '   ';
                    }
                }
            }
        }
        $data['set'] = $data;
        $data['industrytext'] = $industrytext;
        $data['list'] = $list;

        return msg($data, 0, "查询成功");

    }

    public function gettemplatelist($appid,$appSecret)
    {
        $wechatpayService = new WechatpayService();
        $token = $wechatpayService -> getAccessToken($appid,$appSecret);
        $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token=' . $token;
        $c = httpGet($url);

        $result = json_decode($c, true);

        if (! is_array($result)) {
            return false;
        }

        if (! empty($result['errcode'])) {
            return false;
        }

        return $result['data'];
    }


    public function getindustry($appid,$appSecret)
    {
        $wechatpayService = new WechatpayService();
        $token = $wechatpayService -> getAccessToken($appid,$appSecret);
        $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token=' . $token;
        $c = httpGet($url);
        $result = json_decode($c, true);

        if (! is_array($result)) {
            return false;
        }

        if (! empty($result['errcode'])) {
            return false;
        }

        return $result;
    }


    public function save(){
        $request = $this->request;
        $param = $request->param();
        foreach ($param as $key => $value ){
            $TemplateModel  = new \app\admin\model\Template();
            $TemplateInfo = $TemplateModel->where(['name'=> $key,'platform_id' => $this->request->platform_id])->find();
            if ($TemplateInfo){
                $TemplateModel->where(['name'=> $key,'platform_id' => $this->request->platform_id])->update(['template_id' => $value]);
            }else{
                $TemplateModel->save(['name'=> $key,'platform_id' => $this->request->platform_id,'template_id' =>$value ]);
            }

        }
        return msg([], 0, "修改成功");
    }



}